/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2002, GeoVISTA Center
 * Licensed under Simplified BSD License
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
*
*
* @Original Author: jin Chen
* @date: Apr 23, 2005$
* @version: 1.0
*/
package edu.psu.geovista.app.dataloader.v1.xml;

import edu.psu.geovista.app.dataloader.v1.config.model.DfPathInfo;
import edu.psu.geovista.app.dataloader.v1.config.model.Project;
import edu.psu.geovista.app.dataloader.v1.session.ProjectBuilder;
import edu.psu.geovista.common.xml.dom4j.XMLParserDom4j;
import org.dom4j.Document;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class Dom4jPathsXMLFs implements PathsXMLFileSystem  {
    //Document doc;
    File file;
    Project project;

    public Dom4jPathsXMLFs() {
        this.project =new DfPathInfo();
    }

    public void load(){
        try {
            System.out.println(this.getClass().getName() + "XMLParserDom4j.loadDocument(file); -->");
            Document doc = XMLParserDom4j.loadDocument(file);
            System.out.println(this.getClass().getName() + "ready to load doc");
            PathsToDom4j.loadProject(project,doc);
            System.out.println(this.getClass().getName() + "<-- complete load doc");
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }


    public void save() throws FileNotFoundException {
        try {
            File file = getFile();
            //if (file==null||!file.exists() ){    
                file=getDefaultFile();
                this.setFile(file);
            //}
            Document doc= PathsToDom4j.convert(getProject());
            XMLParserDom4j.saveDocument(doc,getFile() );
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public Project getProject() {
        return project;
    }
  
    public void setProject(Project proj) {
        this.project =proj;


    }

   /* public void setProject(Project project) {
        this.project = project;
    }*/

    public File getDefaultFile() throws IOException {

        String id = this.getProject().getID();
        File pathsFile = ProjectBuilder.getPathsFile(id);
        if(!pathsFile.exists() ){
            File projRoot = pathsFile.getParentFile();
            //File projRoot = ProjectBuilder.getProjectRootDir(id);
            if(!projRoot.exists() ){
                projRoot.mkdirs();
            }
            pathsFile.createNewFile() ;
        }
        return pathsFile;

    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public List getSortedProjectIDs() {
        // need go through workspace and list all the dirs PathsToDom4j.loadPcpData();
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }



    public Project getProjectByID(String id) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public Project getProjectByIDIngoreCase(String id) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public List getSortProjectList() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
